﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class ccostoController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /ccosto/
        public ActionResult IndexById(int id)
        {
            var query = entidades.CCOSTOes.Where(cc => cc.IdCCosto == id);
             
            var result = (from cc in query.ToList()
                          join eg in entidades.ELEMENTOGASTOes on cc.IdElemGasto equals eg.IdElemGasto
                          select new
                          {
                              IdCCosto = cc.IdCCosto,
                              Ccosto = eg.NomElemGasto,
                              Codigo = cc.Codigo,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            string elementogc = Convert.ToString(Request.QueryString["query"]);

            var query = entidades.CONVEPROYs
                .Where(cc => cc.IdProyecto == id)
                .Join(entidades.PROYCOMPs, cpy => cpy.IdConvProy, pyc => pyc.IdConvProy, (cpy, pyc) => new { pyc.IdProyComp })
                .Join(entidades.PROYACTs, pyc => pyc.IdProyComp, pya => pya.IdProycomp, (pyc, pya) => new { pya.IdProyAct })
                .Join(entidades.CCOSTOes, pya => pya.IdProyAct, cc => cc.IdProyAct, (pya, cc) => new { cc.IdCCosto, cc.Codigo, cc.IdElemGasto })
                .Join(entidades.ELEMENTOGASTOes, cc => cc.IdElemGasto, eg => eg.IdElemGasto, (cc, eg) => new { cc.IdCCosto, cc.Codigo, cc.IdElemGasto,eg.NomElemGasto });

            if (!String.IsNullOrEmpty(elementogc))
            {
                query = query.Where(cc => (cc.NomElemGasto).ToUpper().Contains(elementogc.ToUpper()) ||
                    (cc.Codigo).ToUpper().Contains(elementogc.ToUpper()));
            }

            var result = (from cc in query.OrderBy(cc => cc.IdCCosto).Skip(start).Take(limit).ToList()
                          select new
                          {
                              IdCCosto = cc.IdCCosto,
                              Ccosto = cc.NomElemGasto,
                              Codigo = cc.Codigo,
                          }).ToList();

            return Json(new { grid = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /ccosto/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ccosto/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ccosto/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ccosto/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /ccosto/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ccosto/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /ccosto/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
